# coding:utf-8
__author__ = '啦拉嘿'
__date__ = '2020/3/5 17:21'
from tkinter import *
import tkinter.ttk as ttk
import random
import string


def random_color():
    ran_str = ''.join(random.sample(string.hexdigits, 6))
    alpha = ''.join(random.sample('ABCDEF', 2))
    return "#" + ran_str + alpha


def random_beautiful_color():
    color = ['#ff0000', '#eb4310', '#f6941d', '#fbb417', '#ffff00', '#cdd541', '#99cc33', '#3f9337', '#219167',
             '#239676', '#24998d', '#1f9baa', '#0080ff', '#3366cc', '#333399', '#003366', '#800080', '#a1488e',
             '#c71585', '#bd2158']
    return color


class TreeViewTable:
    """
    从数据库中索引表头数据并且带滚动条的表格
    更多配置： https://www.cnblogs.com/yang-2018/p/11824250.html
    滚动条支持的 widget ：canvas, entry, listbox, text，treeview
    """
    def __init__(self, master, columns_head: list, table_data: list, text_align='center', width=1, selectmode="extended"):
        self.__master = master
        self.__columns_head = columns_head
        self.__table_data = table_data
        self.__text_align = text_align
        self.__width = width
        self.__selectmode = selectmode
        self.__table = ttk.Treeview(master, columns=self.__columns_head, style='Treeview', show='headings',
                         selectmode=self.__selectmode)
        self.__vertical_bar = ttk.Scrollbar(master, orient=VERTICAL, command=self.__table.yview)
        self.__y_start = 0  # 水平线
        self.__t_relheight = 1

    def __set_table(self):
        # 设置表头名称
        for i in self.__columns_head:
            self.__table.column(i, width=self.__width, anchor=self.__text_align)
            self.__table.heading(i, text=i)
        # 插入数据
        for _ in self.__table_data:
            self.__table.insert('', 'end', values=_)
        # 选中数据后弹框

    def update_data(self, table_data):
        for i in self.__table.get_children():
            self.__table.delete(i)
        for _ in table_data:
            self.__table.insert('', 'end', values=_)

    def get_table(self):
        return self.__table

    def tree_view_table_display(self):
        self.__set_table()
        # 垂直滚动条

        self.__table.configure(yscrollcommand=self.__vertical_bar.set)
        self.__table.pack(side=LEFT, fill=BOTH, expand=True)
        self.__vertical_bar.pack(side=LEFT, fill=Y)
